﻿//  --------------------------------
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://www.codeplex.com/oxite/license
//  ---------------------------------
using System;
using System.Web;
using System.Web.Mvc;
using Oxite.Extensions;
using ECube.Web.CMS.Models;
using System.Web.Script.Serialization;
namespace ECube.Web.CMS.ModelBinders
{
    public class PageProfileModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request = controllerContext.HttpContext.Request;

            
            if (string.Compare(request.HttpMethod, HttpVerbs.Post.ToString(), true) == 0)
            {
                if(request.IsAjaxRequest())
                //if (request.ContentType.Contains("application/json"))
                {
                    string inputContent;
                    using (var sr = new System.IO.StreamReader(request.InputStream))
                    {
                        inputContent = sr.ReadToEnd();
                    }
                   
                    return PageProfile.FromJson(inputContent);

                }              
            }

            return null;
        }
    }
}
